package server.life;

import server.MapleCarnivalFactory;
import server.MapleCarnivalFactory.MCSkill;
import server.maps.MapleMap;
import server.maps.MapleReactor;
import tools.MaplePacketCreator;

import java.awt.*;
import java.util.concurrent.atomic.AtomicInteger;

public class SpawnPoint extends Spawns {
    private MapleMonster monster;
    private Point pos;
    private long nextPossibleSpawn;
    private int mobTime;
    private int carnival, fh, f, id, level = -1;;
    private AtomicInteger spawnedMonsters;
    private boolean immobile;
    private String msg;
    private byte carnivalTeam;

    public SpawnPoint(MapleMonster monster, Point pos, int mobTime, byte carnivalTeam, String msg) {
        this.carnival = -1;
        this.spawnedMonsters = new AtomicInteger(0);
        this.monster = monster;
        this.pos = pos;
        this.mobTime = ((mobTime < 0) ? -1 : (mobTime * 1000));
        this.carnivalTeam = carnivalTeam;
        this.id = monster.getId();
        this.fh = monster.getFh();
        this.f = monster.getF();
        this.msg = msg;
        this.immobile = !monster.getStats().getMobile();
        this.nextPossibleSpawn = System.currentTimeMillis();
    }

    public void setCarnival(int c) {
        this.carnival = c;
    }


    public final void setLevel(int c) {
        this.level = c;
    }

    @Override
    public Point getPosition() {
        return this.pos;
    }

    @Override
    public MapleMonster getMonster() {
        return this.monster;
    }

    @Override
    public byte getCarnivalTeam() {
        return this.carnivalTeam;
    }

    @Override
    public int getCarnivalId() {
        return this.carnival;
    }

    @Override
    public boolean shouldSpawn() {
        if (this.mobTime < 0) {
            return false;
        }
        if (((this.mobTime != 0 || this.immobile) && this.spawnedMonsters.get() > 0) || this.spawnedMonsters.get() > 1) {
            return false;
        }
        long time = System.currentTimeMillis();
        return this.nextPossibleSpawn <= time;
    }

    @Override
    public boolean shouldSpawn2() {
        return this.mobTime >= 0;
    }

    @Override
    public MapleMonster spawnMonster(MapleMap map) {
        (this.monster = new MapleMonster(this.monster)).setPosition(this.pos);
        this.monster.setCarnivalTeam(this.carnivalTeam);
        this.spawnedMonsters.incrementAndGet();
        this.monster.setListener((MonsterListener) new MonsterListener() {
            @Override
            public void monsterKilled() {
                SpawnPoint.this.nextPossibleSpawn = System.currentTimeMillis();
                if (SpawnPoint.this.mobTime > 0) {
                    SpawnPoint.this.nextPossibleSpawn += (long) SpawnPoint.this.mobTime;
                }
                SpawnPoint.this.spawnedMonsters.decrementAndGet();
            }
        });
        map.spawnMonster(this.monster, -2);
        if (this.carnivalTeam > -1) {
            for (MapleReactor r : map.getAllReactorsThreadsafe()) {
                if (r.getName().startsWith(String.valueOf((int) this.carnivalTeam)) && r.getReactorId() == 9980000 + this.carnivalTeam && r.getState() < 5) {
                    int num = Integer.parseInt(r.getName().substring(1, 2));
                    MCSkill skil = MapleCarnivalFactory.getInstance().getGuardian(num);
                    if (skil == null || skil.getMobSkill() == null || this.monster == null) {
                        continue;
                    }
                    skil.getMobSkill().applyEffect(null, this.monster, false);
                }
            }
        }
        if (this.msg != null) {
            map.broadcastMessage(MaplePacketCreator.serverNotice(6, this.msg));
        }
        return this.monster;
    }

    @Override
    public int getMobTime() {
        return this.mobTime;
    }
}
